# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : movie_top_api.py
@Time    : 2020/2/11 0011 10:22
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
from flask import current_app
from flask_restful import Resource, fields, marshal
from sqlalchemy import func

from App.apis.api_constant import HTTP_OK
from App.ext import db
from App.models.cinema_admin.cinma_hall_movie_model import HallMovie
from App.models.common.movie_model import Movie
from App.models.movie_user.movie_order_model import MovieOrder

fileds_top = {
    "id": fields.Integer,
    "name": fields.String,
    "sumprice": fields.Float
}

top_list = {
    "msg": fields.String,
    "status": fields.Integer,
    "data": fields.List(fields.Nested(fileds_top))
}


class MovieTopResource(Resource):
    def get(self):
        results = db.session.query(Movie.id, Movie.showname, func.sum(MovieOrder.o_price)).join(Movie.hall_movies).join(
            HallMovie.h_orders).group_by(Movie.id).order_by(-func.sum(MovieOrder.o_price)).all()
        # 通过query() 中完成对相关字段显示的筛选活动
        dicts = {}
        for result in results:
            dicts = dict(zip(("id", "name", "sumprice"), result))
        current_app.logger.info("simple page info...")
        current_app.logger.warning("warning msg!")
        current_app.logger.error("ERROR!!!!!")
        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": dicts
        }
        return marshal(data, top_list)


if __name__ == "__main__":
    pass
